package code.main.controller.table
{
	import code.module.sound.SoundManager;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Linear;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import ui.card.HideCardBg;
	
	/**
	 *发牌动画控制器 
	 * @author YY
	 * 
	 */
	public class DealCardCtrl
	{
		public var dealCardCB:Function;//发牌回调
		private var _parent:DisplayObjectContainer;//
		private var _seatIds:Array;//要发牌的座位id
		private var _dealCount:int;
		private var _currentCount:int = 0;
		
		private var _pos:Array = [
			[[336,124],[513,64]],
			[[336,124],[541,217]],
			[[336,124],[542,383]],
			[[336,124],[290,450]],
			[[336,124],[16,385]],
			[[336,124],[17,217]],
			[[336,124],[67,60]],
		];
		public function DealCardCtrl(parent:DisplayObjectContainer, callBack:Function)
		{
			this.dealCardCB = callBack;
			_parent = parent;
		}
		
		
		/**
		 *开始发牌   
		 * @param seats 要发牌的座位
		 * 
		 */
		public function dealACard(seat:int, cb:Function):void {
			SoundManager.playerSound(SoundManager.DealSound);
			var c:HideCardBg = new HideCardBg();
			_parent.addChild(c);
			var pos:Array = _pos[seat];
			c.x = pos[0][0];
			c.y = pos[0][1];
			TweenLite.to(
				c, 
				.2, 
				{x:pos[1][0], y:pos[1][1], ease:Bounce.easeOut, onComplete:callBack,onCompleteParams:[c,seat]});
		}
		
		/**
		 *发牌回调 
		 * @param c
		 * @param seatId
		 * 
		 */
		private function callBack(c:DisplayObject,seatId:int):void {
			if(c && c.parent) {
				c.parent.removeChild(c);
			}
			if(Boolean(dealCardCB)) {
				dealCardCB(seatId, int((_currentCount+_seatIds.length-1)/_seatIds.length));
			}
			if(_currentCount < _dealCount) {
				dealACard(_seatIds[_currentCount%_seatIds.length], callBack);
			}
			_currentCount++;
		}
		
		/**
		 *发牌动画
		 * @param seatIds
		 * 
		 */
		public function dealCards(seatIds:Array):void {
			this._seatIds = seatIds;
			this._dealCount = seatIds.length*5;
			_currentCount = 1;
			dealACard(seatIds[0], callBack);
		}
	}
}